<?php

namespace App\Http\Controllers\Admin;

use App\Interfaces\SmsService;
use App\Models\PasswordCoupon;
use App\Models\User;
use App\Repositories\PasswordCouponRepository;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
//use App\Jobs\MassMessage;
use App\Console\Commands\MassMessage;
//use App\Commands\MassMessage;
use App\Exceptions\Interfaces\SmsSendErrorException;
use Illuminate\Support\Facades\DB;


class MassMessageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(PasswordCouponRepository $passwordCouponRepository)
    {
        $commands = $passwordCouponRepository->getCouponNoSend();
        $send_object_arr = ['0'=>'所有用户','1'=>'近三个月登录','2'=>'邮箱认证'];
        //dd($commands);
        return view('admin.coupon.message',compact('commands','send_object_arr'));
    }

    public function sendMassMessage(Request $request, SmsService $smsService,PasswordCouponRepository $passwordCouponRepository,PasswordCoupon $passwordCoupon){

        $command = array();
        $phone = array();
        $command_phone = array();
//dd($request->select_command);

        $coupon_ids = $request->select_command;
        foreach ($coupon_ids as $k=>$v) {
            $password = $passwordCouponRepository->getPwds($v);
            foreach ($password as $k=>$v) {
                $command[]=$v->password;
                //dd($v->password);
            }
        }
        //dd($command);
        $select_user=$request->select_user;
        if($select_user==1){
            $user = User::where('last_login_time','>',date('Y-m-d H:i:s',strtotime('-3 month')))->get();
        }
        if($select_user==2){
            $user = User::where('email', '<>', '')->get();
        }
        if($select_user==3){
            $user = User::where('last_login_time','>',date('Y-m-d H:i:s',strtotime('-3 month')))->where('email', '<>', '')->get();
        }
        foreach ($user as $k=>$v) {
            $phone[]=$v->phone;
            //dd($v->phone);
        }

        if(count($command)>count($phone)){
            //口令多于手机号
            $command = array_slice($command,0,count($phone));
            $message="您选择的口令多余用户，系统将自动为您匹配相应的用户";

        }
        if(count($phone>count($command))){
            //手机号多于口令
            $phone = array_slice($phone,0,count($command));
            $message="您选择的用户多余口令，系统将自动为您匹配相应的用户";

        }
        foreach ($command as $k=>$v) {
            $command_phone[$v]=$phone[$k];
        }

        try {
            foreach ($command_phone as $k=>$v) {
                //$v 电话；$k 口令
                $result = $this->dispatch(new MassMessage($v, '优惠券口令：'.$k,$templateId='', $signature=''));

                if ($result['status']==1000){
                    //改变$k状态
                    $passwordCoupon = PasswordCoupon::find($k);
                    //dd($passwordCoupon);
                    $passwordCoupon->password_status = 1;
                    $passwordCoupon->save();
                    //····
                }else{
                    throw SmsSendErrorException($v.'发送失败');
                }
            }
        }catch (SmsSendErrorException $ex ){
            throw new SmsSendErrorException($ex->getMessage());
        }
        //return redirect()->back()->withInput()->withErrors('该订单不存在！');
        return Redirect('admin/coupon_message');

    }
}
